package 剑指offer;

import java.util.LinkedList;

public class 约瑟夫环 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        long currentTime = System.currentTimeMillis();
        circleOne(arr, 3);
        System.out.println(System.currentTimeMillis() - currentTime+"s");
        for (int i = 0; i < arr.length; i++) {
            linkedList.add(arr[i]);
        }
        currentTime = System.currentTimeMillis();
        circleSecond(arr, 3);
        System.out.println("你好");
        System.out.println(System.currentTimeMillis() - currentTime+"s");
    }

    private static void circleOne(int[] arr, int target) {
        boolean[] flags = new boolean[arr.length];
        int count = 0;
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            if (!flags[i]) {
                count++;
                if (count == target) {
                    flags[i] = true;
                    count = 0;
                    number++;
                    System.out.print(arr[i] + "\t");
                }
            }
            if (i == arr.length - 1) {
                i = -1;
            }
            if (number == arr.length) {
                return;
            }


        }

}

    private static LinkedList<Integer> linkedList = new LinkedList<>();

    private static void circleSecond(int[] arr, int target) {
        int count = 1;
        int index = 0;
        while (!linkedList.isEmpty()) {
            if (count == 3) {
                System.out.print(linkedList.get(index)+"\t");
                linkedList.remove(index);
                index--;
                if (linkedList.size() == 0) {
                    return;
                }
                count = 0;

            }
            count++;
            index++;
            index = (index) % linkedList.size();
        }

    }
}
